<?php

namespace App\Http\Controllers\Admin\Client;

use App\Http\Controllers\Controller;
use App\Http\Requests\client\OpenInfoRequest;
use App\Models\client\OpenInfoModel;
use Illuminate\Http\Request;

class OpenInfo extends Controller
{
    use OpenInfoRequest;

    /**
     * Notes:获取
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request) {

        $info = OpenInfoModel::query()->first();

        if($info){
            $res = [
                'mobile' => $info->mobile,
                'email' => $info->email,
                'wechat' => $info->wechat,
            ];
        }else{
            $res = [
                'mobile' => '',
                'email' => '',
                'wechat' => '',
            ];
        }

        return $this->success($res);
    }

    /**
     * Notes:保存
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $find = OpenInfoModel::find(1);
        if($find){
            $find->mobile = $params['mobile'];
            $find->email = $params['email'];
            $find->wechat = $params['wechat'];
            $find->save();
        }else{
            $model = new OpenInfoModel();
            $model->mobile = $params['mobile'];
            $model->email = $params['email'];
            $model->wechat = $params['wechat'];
            $model->created_admin_id = $auth->id;
            $model->save();
        }

        return $this->success();
    }



}
